package com.kgc.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.pojo.Newscomment;
import com.kgc.service.NewscommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/comment")
public class NewscommentController {

    @Autowired
    private NewscommentService newscommentService;

    @RequestMapping("/list")
    public List<Newscomment> list(){
        List<Newscomment> newscomments=newscommentService.selectNewscommentAll();
        return newscomments;


    }

    @RequestMapping("/comlist")
    public String list(@RequestParam(required = false) Integer newsid,
                       @RequestParam(required = false,defaultValue = "1")Integer pageNo,
                       @RequestParam(required = false,defaultValue = "3")Integer pageSize,
                       Model model, HttpSession session){

        if (newsid == null) {
            newsid = (Integer) session.getAttribute("newsid");
        } else {
            session.setAttribute("newsid", newsid);
        }

        //调
        System.out.println(newsid+"asdwad");
        PageInfo<Newscomment> pageInfo=newscommentService.selectNewscommentByCondition(newsid,pageNo,3);


        //存
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("newsid",newsid);
        session.setAttribute("aaa",newsid);
        //转
        return "jsp/reply";
    }

    //添加回复
    @RequestMapping("/add")
    public String add(Newscomment newscomment, Model model, HttpSession session) {
        session.setAttribute("newsid", newscomment.getNewsid());
        model.addAttribute("newscomments", newscomment);
        newscomment.setCreatedate(new Date());
        boolean b = newscommentService.addNewscomment(newscomment);
        if (!b) {
            model.addAttribute("error", "添加失败");
            return "jsp/invadd";
        }
        return "redirect:/comment/comlist";
    }
}
